/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Advertise;
import java.awt.Window;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.FlowEvent;
import org.primefaces.model.UploadedFile;
import radio.entities.Accounts;
import radio.entities.PaymentDetails;
import radio.entities.PaymentDetailsPK;
import radio.entities.PaymentRate;
import radio.sessions.AccountsFacade;
import radio.sessions.AdvertiseFacade;
import radio.sessions.AdvertiseTypeFacade;
import radio.sessions.PaymentDetailsFacade;
import radio.sessions.PaymentRateFacade;

/**
 *
 * @author Kim
 */
public class RegisAdvertiseWizard {
    @EJB
    private AdvertiseTypeFacade advertiseTypeFacade;
    @EJB
    private AdvertiseFacade advertiseFacade;
@EJB
    private PaymentDetailsFacade paymentDetailsFacade;
    @EJB
    private PaymentRateFacade paymentRateFacade;

    @EJB
    private AccountsFacade accountsFacade;

    /** Creates a new instance of RegisCompanyWizard */
    private Advertise advertise=new Advertise();

    public Advertise getAdvertise() {
        return advertise;
    }

    public void setAdvertise(Advertise advertise) {
        this.advertise = advertise;
    }
    private String plan;
    private boolean skip;
    public String getPlan() {
        return plan;
    }
    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }
    public void setPlan(String plan) {
        this.plan = plan;
    }
    
    /** Creates a new instance of RegisAdvertiseWizard */
    public RegisAdvertiseWizard() {
    }
    private static Log logger = LogFactory.getLog(RegisAdvertiseWizard.class); 
    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void upload() {
        FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void handleFileUpload(FileUploadEvent event) {
//        if (event.getFile().getSize() > (1024 * 1024 * 2)) {
//            FacesMessage msg = new FacesMessage("File size must greater 2MB");
//            FacesContext.getCurrentInstance().addMessage(null, msg);
//            System.out.println(Window.getWindows());
//            
//        } else {
        logger.info("Uploaded: {}"+ event.getFile().getFileName());
            file=event.getFile();
            System.out.println(file.getFileName()+ "asdfasdfas");
            FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            advertise.setImg("./../images/user/" + event.getFile().getFileName());
            System.out.println(advertise.getImg());
//        }
    }    

    public void save() {
        try {
            
            HttpSession ses=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            String userid=String.valueOf(ses.getAttribute("userId"));
            Accounts acc=accountsFacade.find(Integer.parseInt(userid));
            
            if(checkupgrade(acc))    
            {
//            AccountsType type=accountsTypeFacade.getId("advertise");            
//            acc.setTypeId(type);                        
            
            
                        
            radio.entities.Advertise ad=new radio.entities.Advertise(acc.getAccId(), advertise.getCompanyname(), advertise.getDesignation(), advertise.getFax(), advertise.getDescription());
            radio.entities.AdvertiseType type=advertiseTypeFacade.find(1);
            ad.setAdvertiseTypeId(type);
            advertiseFacade.create(ad);
            PaymentRate pay=null;                  
            if(plan.equalsIgnoreCase("month")){
            pay=paymentRateFacade.findid("Advertise", 30);
                    }else{
           pay=paymentRateFacade.findid("Advertise", 90);    
            }
            PaymentDetailsPK pk=new PaymentDetailsPK(acc.getAccId(), pay.getPaymentId(),new java.util.Date());
            PaymentDetails paymentdetail=new PaymentDetails(pk, pay.getPaymentPrice(), false);
            paymentDetailsFacade.create(paymentdetail);
            FacesMessage msg = new FacesMessage("Successful", "You have been registered to Advertisement");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            }else{
                FacesMessage msg = new FacesMessage("Upgrade Fail","Upgrade Fail.You have upgraded already.");            
            FacesContext.getCurrentInstance().addMessage(null, msg);
            }
        } catch (Exception e) {
            FacesMessage msg = new FacesMessage("Upgrade Fail");
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    public boolean checkupgrade(Accounts acc){       
        PaymentDetails det=paymentDetailsFacade.checkupgrade(acc.getAccId());           
        if(det==null)
            return true;
        else return false;
    }
    

    public String onFlowProcess(FlowEvent event) {
        System.out.println(Window.getOwnerlessWindows());
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());

        if (skip) {
            skip = false;
            return "confirm";
        } else {
            return event.getNewStep();
        }
    }
}
